package md.lanjing.com.module_deal_center.ui.activity;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.net.ServiceGenerator;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.TimeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import butterknife.BindView;
import butterknife.OnClick;

import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import com.lxh.baselibray.base.BaseResponce;
import md.lanjing.com.module_deal_center.bean.CheckOrderDetailResponce;
import com.lxh.baselibray.constant.Constant;
import md.lanjing.com.module_deal_center.contact.CheckOrderDetailContact;
import retrofit2.Response;

/**
 * 审核订单详情
 */
public class CheckOrderDetailActivity extends MvpActivity<CheckOrderDetailContact.CheckOrderDetailPresent> implements CheckOrderDetailContact.ICheckOrderDetailView {
    public static final String TRANSFER_ID = "transferId";
    @BindView(R2.id.tv_order_number)
    TextView tvOrderNumber;
    @BindView(R2.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R2.id.tv_coin_type)
    TextView tvCoinType;
    @BindView(R2.id.iv_pay_type)
    ImageView ivPayType;
    @BindView(R2.id.tv_buy_id)
    TextView tvBuyId;
    @BindView(R2.id.tv_buy_number)
    TextView tvBuyNumber;
    @BindView(R2.id.tv_per_price)
    TextView tvPerPrice;
    @BindView(R2.id.tv_buy_total_price)
    TextView tvBuyTotalPrice;
    @BindView(R2.id.photoView)
    PhotoView phontoView;

    String transferId;
    String billId;
    @Override
    public void initData(Bundle savedInstanceState) {
         transferId = getIntent().getStringExtra(TRANSFER_ID);
        mPresent.checkBillDetail(context,transferId);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_check_order_detail;
    }


    @OnClick(R2.id.btn_confirm)
    public void onViewClicked() {
        showDealDialog();
    }

    AlertDialog dealDialog = null;
    private void showDealDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd=psw.getPsw();
                    mPresent.confirmCheckOrder(context,billId,transferId,dealPwd);
                    dealDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }
    @Override
    public void getCheckOrderDetailData(Response<CheckOrderDetailResponce> response) {
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            CheckOrderDetailResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data))return;
            billId=data.getBillId();
            tvOrderNumber.setText("订单号："+data.getBillId());
            tvOrderTime.setText("订单时间："+ TimeUtils.millis2String(Long.parseLong(data.getBuyTime())));
            tvCoinType.setText("币种："+data.getType());
            tvBuyId.setText("购买人ID："+data.getBuyerId());
            tvBuyNumber.setText("购买数量："+data.getBuyTokenNum());
            tvPerPrice.setText("单价："+data.getCnyPrice()+"CNY");
            tvBuyTotalPrice.setText("总价："+Double.valueOf(data.getCnyPrice())*Double.valueOf(data.getBuyTokenNum())+"CNY");
            Glide.with(context).load(ServiceGenerator.BASE_URL+":8089/image/"+data.getTransactionPic()).into(phontoView);
            if (ObjectUtils.equals("1",data.getAliPay())){
                ivPayType.setImageResource(R.drawable.icon_ali);
            }
            if (ObjectUtils.equals("1",data.getWeChat())){
                ivPayType.setImageResource(R.drawable.icon_weinxin);
            }
            if (ObjectUtils.equals("1",data.getBank())){
                ivPayType.setImageResource(R.drawable.icon_card);
            }
      }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    public void getConfirmOrderData(Response<BaseResponce> response) {
        if (response.body().getCode()==Constant.SUCCESS_CODE){
            ToastUtils.showLongToast(context,"审核通过");
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected CheckOrderDetailContact.CheckOrderDetailPresent createPresent() {
        return new CheckOrderDetailContact.CheckOrderDetailPresent();
    }
}
